package com.supreme.simpleserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonmybatis.entity.primary.GroupGroupBy;
import com.supreme.commonmybatis.entity.primary.MBGroup;
import com.supreme.commonmybatis.mapper.GroupGroupByMapper;
import com.supreme.simpleserver.service.GroupService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "group")
public class GroupController {

    @Resource
    private GroupGroupByMapper groupGroupByMapper;

    @Resource
    private GroupService groupService;

    @GetMapping(value = "selectList")
    public AjaxResp selectList() {
        List<MBGroup> groupList = groupService.list();
        return AjaxResp.instance(groupList);
    }

    @GetMapping(value = "selectGroup")
    public AjaxResp selectGroup() {
        LambdaQueryWrapper<GroupGroupBy> queryWrapper = Wrappers.lambdaQuery(GroupGroupBy.class).groupBy(GroupGroupBy::getGroupType);
        List<GroupGroupBy> groupGroupByList = groupGroupByMapper.selectList(queryWrapper);
        return AjaxResp.instance(groupGroupByList);
    }

    @PostMapping(value = "insertRandomList")
    public AjaxResp insertRandomList() {
        final int batchCount = 100;
        List<MBGroup> groupList = new ArrayList<>(batchCount);
        for (int i = 0; i < batchCount; i++) {
            String groupCode = RandomStringUtils.randomNumeric(16);
            String groupName = RandomStringUtils.randomAlphabetic(16).toLowerCase();
            int groupType = RandomUtils.nextInt(1, 4);
            int groupStatus = RandomUtils.nextInt(0, 9);
            MBGroup group = new MBGroup()
                    .setGroupCode(groupCode)
                    .setGroupName(groupName)
                    .setGroupType(groupType)
                    .setGroupStatus(groupStatus);
            groupList.add(group);
        }
        boolean saveBatch = groupService.saveBatch(groupList, batchCount);
        return AjaxResp.instance(saveBatch);
    }
}
